<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item
          v-for="(item, index) in breadList"
          :key="index"
          :to="{ path: item.path }"
        >
          {{ item.meta.title }}
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div>
        <el-form-item label="景点">
          <el-select v-model="region" placeholder="请选择景点" @change="selectedRegionName">
          <!-- 从景点表中获取景点信息 -->
          <el-option 
            v-for="item in travel"
            :key="item.id"
            :label="item.name"
            :value="{ id: item.id, name: item.name }" 
          />
          </el-select>
        </el-form-item>
        <el-form-item label="购买数量">
          <el-input-number v-model="num" :min="1" :max="10" @change="handleChange" />
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="input1" style="width: 500px" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="input2" style="width: 500px" placeholder="请输入" />
        </el-form-item>
      </div>
      <div class="mb-4">
        <el-button type="primary" @click="submintData">添加</el-button>
      </div>
    </div>
    
  </div>
</template>

<script setup>
import { useRouter, useRoute } from "vue-router";
import { ref, reactive, watch ,computed} from "vue";
import request from "../../utils/request";
import { ElMessage, ElNotification } from "element-plus";

let breadList = []; // 路由集合
const route = useRoute();
const router = useRouter();

const getBreadcrumb = (url) => {
  let matched = url.matched;
  //如果不是首页
  if ("Home" == matched[0].name) {
    matched[0].meta = { title: "系统首页" };
  }
  breadList = matched;
};
watch(
  route,
  (newValue, oldValue) => {
    // console.log(222222,newValue)
    getBreadcrumb(newValue);
  },
  { deep: true, immediate: true }
);


// 从景点模型中获取景点
let travel = reactive([])
const getTravel = async()=>{
  request.request({
    url:"/order/gettravel",
    method:'get'
  }).then((res)=>{
    // 将获取到的数据添加进数组
    if (res.data.code === 200) {  
            console.log(res.data.data);

            travel.splice(0,travel.length)
            res.data.data.forEach(item=>{
              travel.push({
                id: item.travelId,  
                name: item.name
              })
            })
    
        } else {  
            console.log('获取失败');  
        }  
  }).catch(error =>{
    console.log('请求出错',error);
  })
}
getTravel()
console.log(travel,'1111111');

// 描述、备注输入框
const input1 = ref('')
const input2 = ref('')

// 选取景点
let region = ref({id:null,name:''})
let regioncopy = ref(null)
const selectedRegionName = ()=>{
  regioncopy.value = region.value
  return region.value=region ? region.value.name : '';
}

// 购买数量
const num = ref(1)
const handleChange = (value) => {
  console.log(value)
}

// 提交

const submintData = async () => {  
  if (region.value && num.value && input1.value && input2.value) {  
    const { id: travelId, name: travelName } = regioncopy.value;  
      
    request.request({  
      url: '/order/addorder',  
      method: 'post',  
      data: {  
        travelId: travelId,  
        name: travelName,    
        number: num.value,  
        description: input1.value,  
        content: input2.value  
      }  
    }).then((res) => {  
      if (res.data.code === 200) {  
        console.log('添加成功');  
        ElMessage.success('订单添加成功！');  
      } else {  
        console.log('添加失败', res);  
        ElMessage.error('添加订单失败，请重试！');  
      }  
    }).catch((error) => {  
      console.error('请求失败', error);  
      ElMessage.error('请求失败，请检查网络连接！');  
    });  
  } else {  
    ElMessage.warning('请填写所有必填项！');  
  }  
}

</script>

<style>
.el-form-item{
  margin-bottom: 30px ;
}
</style>
